package com.java46.struts2.Food.action;

import com.java46.struts2.Food.service.FoodService;
import com.java46.struts2.base.action.BaseAction;
import com.java46.struts2.base.entity.PageEntity;
import com.java46.struts2.base.entity.TDishesEntity;
import com.java46.struts2.base.exception.BusinessException;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;

import java.math.BigDecimal;
import java.util.List;
/**
 * @ClassName FoodAction
 * @Description 菜品的Action路径操作
 * @Author Tigger Wu
 * @Date 2018/7/16 19:33
 * @Version 1.0
 **/
public class FoodAction  extends BaseAction {
    private FoodService foodService = new FoodService();
    @Action(value = "queryFoods")
    public String queryTypes(){
        List<TDishesEntity> dlist = (List<TDishesEntity>)foodService.queryAll();


        setSuccesResult(dlist);
        return "json";
    }


    /*
     *  添加菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 16:00
     * @param
     * @return
     */

//    @Action(value = "saveFoods")
//    public String saveFood(){
//        TDishesEntity foods = new TDishesEntity();
//        foods.setdImg("xx");
//        foods.setdName("DDD");
//        foods.setdPrice(new BigDecimal(12.00));
//        foods.setdRemark("dddd");
//        foodService.addFood(foods);
//        return "json";
//    }

    /*
     *
     *
     * @author Tigger Wu
     * @date 2018/7/17 16:00
     * @param
     * @return
     */
//    @Action(value = "removeFoods")
//    public String removeFoods(int id){
//        foodService.removeFoods(id);
//        return "json";
//    }
    /*
     * 修改菜品
     *
     * @author Tigger Wu
     * @date 2018/7/17 16:01
     * @param
     * @return
     */
    @Action(value = "updateFoods")
    public String updateFoods(TDishesEntity foods){
        foodService.updateFoods(foods);
        return "json";
    }
/**
 *  根据id查询菜品
 *
 * @author TiggerWu
 * @date 2018/7/18   17:23
 * @param
 * @return
 */
@Action(value = "queryByFoodsId")
    public String queryById(){
      int id = Integer.parseInt(request.getParameter("foodId"));
      List<TDishesEntity> foods = (List<TDishesEntity>)foodService.querybyId(id);
        setSuccesResult(foods);
        return "json";
    }

    @Action(value = "queryFood")
    /**
     *@Author xiao
     *@Description //分页查询订单可加关键字
     *@Date 9:28 2018/7/18
     *@Param []
     *@return void
     */
    public String queryFoods(){
        String pagesize="";
        //获取页条数
        try {
            pagesize = request.getParameter("pagesize");
        }catch (Exception e){
            pagesize="";
        }
        String keyword="";
        //获取关键字
        try {
            keyword = request.getParameter("keyword");
        }catch (Exception e){
            keyword="";
        }
        String pageno="";
        //获取页码
        try {
            pageno = request.getParameter("pageno");
        }catch (Exception e){
            pageno="1";
        }
        PageEntity pageEntity = FoodService.queryFoods(pageno, pagesize, keyword);
        setSuccesResult(pageEntity);
        return "json";
    }

    /**
     *@Author xiao
     *@Description //根据菜品编号删除菜品类型
     *@Date 11:38 2018/7/18
     *@Param []
     *@return void
     */
    @Action(value = "deleteFood")
    public void deleteFood(){
        String foodid=null;
        //获取前台订单编号
        try {
            foodid = request.getParameter("foodid");
        }catch (Exception e){
            throw new BusinessException("没有接收到菜品类型数据");
        }
        FoodService.deleteFood(foodid);
    }


    /**
     * @Author xiaobo
     * @Description //添加菜品
     * @Date 16:56 2018/7/22 0022
     * @Param []
     * @return void
     */
    @Action(value = "addFood",results = {
            @Result(name = "success",type = "redirect", location = "/index.jsp"),
            @Result(name = "error",location = "/food.jsp")
    })
    public String addType(){
        String food_name="";
        BigDecimal food_price;
        String food_remark="";
        String file="";
        Integer typeId;
        try {
            food_name = request.getParameter("food_name");
            food_price = new BigDecimal(request.getParameter("food_price"));
            file = request.getParameter("file");
            typeId = Integer.parseInt(request.getParameter("mySel"));
        }catch (Exception e){
            throw new BusinessException("没有接收到菜品类型数据");
        }
        try {
            food_remark = request.getParameter("food_remark");
        }catch (Exception e){
            throw new BusinessException("没有接收到菜品类型数据",e);
        }
        TDishesEntity foods = new TDishesEntity();
        foods.setdId(0);
        foods.setdRemark(food_remark);
        foods.setdImg(file);
        foods.setdPrice(food_price);
        foods.setdName(food_name);
        foods.setdTypeId(typeId);
        foodService.addFood(foods);
        setSuccesResult("添加成功!");
        return "success";
    }

}